package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.RedisUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.constant.CiccTableNameEnum;
import com.chinairi.mall.controller.viewobject.*;
import com.chinairi.mall.dao.*;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.*;
import com.chinairi.mall.service.IProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinairi.mall.service.modelobject.*;
import com.chinairi.mall.util.CiccRecordUtil;
import com.chinairi.mall.util.VerificationCodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 项目表 服务实现类
 * </p>
 *
 * @author gsl   whn
 * @since 2020-08-23 10-17
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ServiceOrderProjectMapper serviceOrderProjectMapper;
    @Autowired
    private UserCompanyInfoServiceImpl userCompanyInfoService;
    @Autowired
    private UserCompanyRelaMapper userCompanyRelaMapper;
    @Autowired
    private CompShopMapper compShopMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private ContractMaitainMapper contractMaitainMapper;
    @Autowired
    private OrderServicePurchaseMapper purchaseMapper;
    @Autowired
    private OrderServiceQualityMapper qualityMapper;
    @Autowired
    private OrderServiceSettleMapper settleMapper;
    @Autowired
    private OrderServiceStorageMapper storageMapper;
    @Autowired
    private OrderServiceTransportMapper transportMapper;

    /**
     * 根据采购计划编号获取项目信息
     */
    public void putRedis() {
        redisUtil.del("purchaseToProjectMap");
        List<PurchaseToProjectRedisMO> purchaseToProjectRedisMoList = projectMapper.selectPurchaseToProjectRedis();
        Map<String, Object> purchaseToProjectMap = new HashMap<>(purchaseToProjectRedisMoList.size());
        for (PurchaseToProjectRedisMO purchaseToProjectRedisMo : purchaseToProjectRedisMoList) {
            purchaseToProjectMap.put(purchaseToProjectRedisMo.getPlanPurchaseId(), purchaseToProjectRedisMo);
        }
        redisUtil.hmset("purchaseToProjectMap", purchaseToProjectMap, 86500);
    }

    /**
     * 根据采购计划编号获取项目信息
     */
    public PurchaseToProjectRedisMO getPurchaseToProjectFromRedis(String planPurchaseId) {
        Map<Object, Object> project = redisUtil.hmget("purchaseToProjectMap");
        if (!redisUtil.hasKey("purchaseToProjectMap") || project.size() == 0 || redisUtil.hmget("purchaseToProjectMap").get(planPurchaseId) == null) {
            putRedis();
        }
        return (PurchaseToProjectRedisMO) redisUtil.hmget("purchaseToProjectMap").get(planPurchaseId);
    }

    public void putProjectRedis() {
        redisUtil.del("projectMap");
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        List<Project> projectList = projectMapper.selectList(queryWrapper);
        Map<String, Object> projectMap = new HashMap<>(projectList.size());
        for (Project project : projectList) {
            projectMap.put(project.getpId(), project);
        }
        redisUtil.hmset("projectMap", projectMap, 86500);
    }

    public Project getProjectFromRedis(String pId) {
        Map<Object, Object> objectMap = redisUtil.hmget("projectMap");
        if (!redisUtil.hasKey("projectMap") || objectMap.size() == 0) {
            putProjectRedis();
        }
        return (Project) redisUtil.hmget("projectMap").get(pId);
    }

    /**
     * 新增项目
     *
     * @param projectVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProject(ProjectVO projectVo) {
        String pId = UUID.randomUUID().toString().replace("-", "");
        Project project = new Project();
        BeanUtils.copyProperties(projectVo, project);
        project.setpId(pId);
        if (project.getpLevel() == 1) {
            project.setpParentId("0");
        }
        project.setpStatus("0");
        project.setCreateUser(UserSession.getUserName());
        project.setCreateTime(new Date());
        project.setUpdateUser(UserSession.getUserName());
        project.setUpdateTime(new Date());
        projectMapper.insert(project);

    }

    /**
     * 将某个项目添加进项目服务中心
     *
     * @param projectVo
     */
    @Override
    public void addService(ProjectVO projectVo) {
        Project project = projectMapper.selectById(projectVo.getpId());
        //当这个项目选择了项目服务时，要在项目服务的订单表里插入一条记录
        setServiceOrderProject(project);
        project.setpHasService(1);
        project.setpStatus("1");
        projectMapper.updateById(project);
    }

    private void setServiceOrderProject(Project project) {
        Map<Object, Object> map = userCompanyInfoService.getAdminInfoFromRedis();
        ServiceOrderProject serviceOrderProject = new ServiceOrderProject();
        serviceOrderProject.setSopId(VerificationCodeUtil.DateCreate(new Date()));
        serviceOrderProject.setCsDbid((String) map.get("csDbid1"));
        serviceOrderProject.setpId(project.getpId());
        serviceOrderProject.setSopType("1".equals(project.getpType()) ? "井工程项目精细化管理服务" : "地建项目生命周期管理服务");
        serviceOrderProject.setSopUserUnit(project.getpCompany());
        serviceOrderProject.setSopProjectName(project.getpName());
        serviceOrderProject.setCreateUser(UserSession.getUserId());
        serviceOrderProject.setSopStatus(1);
        serviceOrderProject.setpWellNum(project.getpWellNum());
        serviceOrderProject.setpWellClass(project.getpWellClass());
        serviceOrderProject.setpWellDepth(project.getpWellDepth());
        serviceOrderProject.setpWellType(project.getpWellType());
        serviceOrderProject.setpWorkArea(project.getpWorkArea());
        serviceOrderProject.setCreateTime(new Date());
        serviceOrderProject.setSopPrice("1%");
        serviceOrderProject.setSopUnit("周期");
        serviceOrderProject.setSopNumber(1);
        serviceOrderProjectMapper.insert(serviceOrderProject);
    }

    /**
     * 更新
     *
     * @param projectVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProject(ProjectVO projectVo) {
        if (projectVo.getpId() == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "没有指定数据id");
        }
        Project project = new Project();
        BeanUtils.copyProperties(projectVo, project);
        projectMapper.updateById(project);
    }

    /**
     * 删除某个项目
     * 同时删除下面的子项目
     *
     * @param pId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProject(String pId) {
        ArrayList<String> pIdList = new ArrayList<>();
        pIdList.add(pId);
        this.getIds(pIdList, pId);
        projectMapper.deleteBatchIds(pIdList);
    }

    /**
     * 递归获取
     *
     * @param ids
     * @param oneId
     */
    private void getIds(ArrayList<String> ids, String oneId) {
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        wrapper.eq("P_PARENT_ID", oneId);
        List<Project> projectList = projectMapper.selectList(wrapper);

        //遍历二级分类的对象，把二级分类的id加入到要删除的集合中
        for (Project project : projectList) {
            String id = project.getpId();
            ids.add(id);
            this.getIds(ids, id);
        }

    }

    /**
     * 根据条件查询项目
     * 根据项目名称
     * 根据企业id
     *
     * @param projectVo
     * @return
     */
    @Override
    public List<Project> selectProject(ProjectVO projectVo) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        String projectName = projectVo.getpName();
        String uciDbid = projectVo.getUciDbid();
        queryWrapper
                .like((projectName != null) && (!projectName.trim().isEmpty()), "P_NAME", projectName)
                .eq((uciDbid != null) && (!uciDbid.trim().isEmpty()), "UCI_DBID", uciDbid);
        return projectMapper.selectList(queryWrapper);
    }

    /**
     * 获取企业下项目列表
     *
     * @return
     */
    @Override
    public List<ProjectVO> getList(ProjectVO projectVO) {
        //这里可以用redis取数据
        UserInfoToRedisMO userInfoToRedisMO = sysUserService.getUserInfoFromRedis(UserSession.getUserId());
        if (userInfoToRedisMO == null || "".equals(userInfoToRedisMO.getUciDbid())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "您暂时没有企业信息，请先申请企业资质！");
        }
        projectVO.setUciDbid(userInfoToRedisMO.getUciDbid());
        //查询出所有项目数据
        Integer uciCompCharacter = contractMaitainMapper.selectUciCompCharacter(UserSession.getUserId());
        List<ProjectVO> projectVOList = projectMapper.selectByUciDbId(projectVO,uciCompCharacter);
//        getChildListByPId(projectVOList);
//        return projectVOList;
        return projectVOList.stream().filter(vo -> "0".equals(vo.getpParentId()))
                .peek(vo -> vo.setChildren(getChildList(vo, projectVOList)))
                .collect(Collectors.toList());
    }

    private void getChildListByPId(List<ProjectVO> projectVOList) {
        for (ProjectVO projectVO1 : projectVOList) {
            List<ProjectVO> projectVOList1 = projectMapper.selectChildListByPId(projectVO1.getpId());
            if (projectVOList1.size() > 0) {
                projectVO1.setChildren(projectVOList1);
                getChildListByPId(projectVOList1);
            }
        }
    }

    /**
     * 根据当前类别，找出子类，并通过递归找出子类的子类
     *
     * @param projectVO
     * @param projectVOList
     * @return
     */
    private List<ProjectVO> getChildList(ProjectVO projectVO, List<ProjectVO> projectVOList) {
        return projectVOList.stream().filter(vo -> vo.getpParentId().equals(projectVO.getpId()))
                .peek(vo -> vo.setChildren(getChildList(vo, projectVOList)))
                .collect(Collectors.toList());
    }

    /**
     * 获取库点
     *
     * @return
     */
    @Override
    public List<StorageAddressVO> getStorage() {
        return projectMapper.selectStorage();
    }

    public String getUciDbId(String userId) {
        QueryWrapper<UserCompanyRela> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotEmpty(UserSession.getUserId()), "USER_ID", userId);
        UserCompanyRela userCompanyRela = userCompanyRelaMapper.selectOne(wrapper);
        return userCompanyRela.getUciDbid();
    }

    /**
     * 获取所有企业的项目信息
     *
     * @param projectInfoOfAllCompMo
     * @return
     */
    @Override
    @DoPage
    public List<ProjectInfoOfAllCompMO> getProjectInfoOfAllComp(ProjectInfoOfAllCompMO projectInfoOfAllCompMo) {
        if (projectInfoOfAllCompMo.getFlag() == 1) {
            String uciDbId = getUciDbId(UserSession.getUserId());
            if (uciDbId == null || "".equals(uciDbId)) {
                return null;
            } else {
                projectInfoOfAllCompMo.setUciDbid(uciDbId);
            }
        }
        List<ProjectInfoOfAllCompMO> list = projectMapper.getProjectInfoOfAllComp(projectInfoOfAllCompMo);
        for (ProjectInfoOfAllCompMO info : list) {
            info.setBelongTo(projectMapper.getBelongTo(info.getUciDbid()));
            //项目概算金额
            info.setPGeneralBudget(projectMapper.getGeneralBudgetAmountByUciDbId(info.getUciDbid()));
            //需求总数
            info.setTotalPlanReqNum(projectMapper.getAllPlanReqNumByUciDbId(info.getUciDbid()));
            //订单总数
            info.setTotalOrderNum(projectMapper.getTotalOrderNum(info.getUciDbid()));
            //获取时效正常、异常数
            info.setTimeNormalNum(projectMapper.getTimeNormalNumByUciDbId(info.getUciDbid()));
            info.setTimeAbnormalNum(projectMapper.getTimeAbnormalNumByUciDbId(info.getUciDbid()));
            //获取合格、不合格物资数及总数
            info.setQualifiedMaterialNum(projectMapper.getMaterialNumByUciDbId(info.getUciDbid(), "01"));
            info.setUnQuaMaterialNum(projectMapper.getMaterialNumByUciDbId(info.getUciDbid(), "02"));
            info.setTotalMaterialNum(info.getQualifiedMaterialNum() + info.getUnQuaMaterialNum());
        }
        return list;
    }

    /**
     * 根据企业名称查找项目及订单数/花费数
     *
     * @param allInfoMo
     * @return
     */
    @Override
    @DoPage
    public List<AllInfoMO> getAllInfoOfOrder(AllInfoMO allInfoMo) {
        List<AllInfoMO> allInfoMOList = projectMapper.selectAllInfoOfProject(allInfoMo);
        for (AllInfoMO allInfoMO : allInfoMOList) {
            allInfoMO.setInventoryAmount(allInfoMO.getOrderStorageMoney().subtract(allInfoMO.getOrderDeliveryMoney()));
            //遍历查询项目下的订单是否有未完成
            allInfoMO.setState(projectMapper.getUnDoneOrderByPId(allInfoMO.getPId()) > 0 ? 1 : 0);
            //获取已完成、未完成订单数及订单总数
            allInfoMO.setDoneOrderNum(orderInfoMapper.getDoneOrderNum(allInfoMO.getPId()));
            allInfoMO.setUnDoneOrderNum(orderInfoMapper.getUnDoneOrderNum(allInfoMO.getPId()));
            allInfoMO.setOrderCount(allInfoMO.getDoneOrderNum() + allInfoMO.getUnDoneOrderNum());
            //获取已完成、未完成需求计划数及需求计划总数
            allInfoMO.setUnDonePlanReqNum(projectMapper.getUnDonePlanReqNumber(allInfoMO.getPId()));
            allInfoMO.setTotalPlanReqNum(projectMapper.getPlanReqNumber(allInfoMO.getPId()));
            allInfoMO.setDonePlanReqNum(allInfoMO.getTotalPlanReqNum() - allInfoMO.getUnDonePlanReqNum());
            //获取时效正常、异常数
            allInfoMO.setTimeNormalNum(projectMapper.getTimeNormalNum(allInfoMO.getPId()));
            allInfoMO.setTimeAbnormalNum(projectMapper.getTimeAbnormalNum(allInfoMO.getPId()));
            //获取合格、不合格物资数及总数
            allInfoMO.setQualifiedMaterialNum(projectMapper.getMaterialNum(allInfoMO.getPId(), "01"));
            allInfoMO.setUnQuaMaterialNum(projectMapper.getMaterialNum(allInfoMO.getPId(), "02"));
            allInfoMO.setTotalMaterialNum(allInfoMO.getQualifiedMaterialNum() + allInfoMO.getUnQuaMaterialNum());
        }
        return allInfoMOList;
    }

    /**
     * 用于订单详情展示
     *
     * @param allInfoOfOrderMo
     * @return
     */
    @Override
    @DoPage
    public List<AllInfoOfOrderMO> getAllInfoOfOrder(AllInfoOfOrderMO allInfoOfOrderMo) {
        if (allInfoOfOrderMo.getFlag() == 1) {
            CompShop compShop = compShopMapper.getByType(allInfoOfOrderMo.getType(), UserSession.getUserId());
            if (compShop == null) {
                throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "您暂时没有店铺，请先申请店铺！");
            } else {
                allInfoOfOrderMo.setOrderShopId(compShop.getCsDbid());
            }
        }
        return projectMapper.selectAllInfoOfOrder(allInfoOfOrderMo);
    }

    /**
     * 获取某个订单下的实物商品
     *
     * @param goodsInfoOfOrderMo
     * @return
     */
    @Override
    @DoPage
    public List<GoodsInfoOfOrderMO> getGoodsInfoOfOrder(GoodsInfoOfOrderMO goodsInfoOfOrderMo) {
        return projectMapper.getGoodsInfoOfOrder(goodsInfoOfOrderMo);
    }

    /**
     * 获取某个物料的生产信息
     *
     * @param productionInfoOfOrderMo
     * @return
     */
    @Override
    @DoPage
    public List<ProductionInfoOfOrderMO> getProductionInfoOfOrder(ProductionInfoOfOrderMO productionInfoOfOrderMo) {
        return projectMapper.getProductionInfoOfOrder(productionInfoOfOrderMo);
    }

    /**
     * 根据订单id，获取该订单商品
     *
     * @param orderId
     * @return
     */
    @Override
    public OrderInforGoodsVO getGoodsByOrderId(String orderId) {
        OrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        CiccRecord ciccRecord = CiccRecordUtil.getCiccRecord(CiccTableNameEnum.ORDER_CONFIRM_STATUS.getCodeValue(), String.valueOf(orderInfo.getOrderState()));
        orderInfo.setOrderStateLabel(ciccRecord.getLabel());
        OrderInforGoodsVO orderInforGoodsVO = new OrderInforGoodsVO();
        BeanUtils.copyProperties(orderInfo, orderInforGoodsVO);
        return orderInforGoodsVO;
    }

    @Override
    @DoPage
    public List<OrderGoodsVO> getGoodsListByOrderId(String orderId) {
        return projectMapper.getGoodsByOrderId(orderId);
    }

    @Override
    public AllProjectInfoOfCompany getAllProjectInfoOfCompany(String uciDbId) {
        AllProjectInfoOfCompany info = projectMapper.getAllProjectInfoOfCompany(uciDbId);
        //获取五大服务订单数量及金额
        AllProjectInfoOfCompany purchaseInfo = purchaseMapper.getServiceNumAndMoneyByUciDbId(uciDbId);
        AllProjectInfoOfCompany settleInfo = settleMapper.getServiceNumAndMoneyByUciDbId(uciDbId);
        AllProjectInfoOfCompany storageInfo = storageMapper.getServiceNumAndMoneyByUciDbId(uciDbId);
        AllProjectInfoOfCompany transportInfo = transportMapper.getServiceNumAndMoneyByUciDbId(uciDbId);
        AllProjectInfoOfCompany qualityInfo = qualityMapper.getServiceNumAndMoneyByUciDbId(uciDbId);
        //汇总服务订单数量
        info.setServicesNumber(purchaseInfo.getServicesNumber() + settleInfo.getServicesNumber() + storageInfo.getServicesNumber() +
                transportInfo.getServicesNumber() + qualityInfo.getServicesNumber());
        //汇总服务订单金额
        info.setTotalServiceAmount(purchaseInfo.getTotalServiceAmount().add(settleInfo.getTotalServiceAmount()).add(storageInfo.getTotalServiceAmount())
                .add(transportInfo.getTotalServiceAmount()).add(qualityInfo.getTotalServiceAmount()));
        info.setTotalPlanReqNum(projectMapper.getAllPlanReqNumByUciDbId(uciDbId));
        info.setTotalProjectNum(projectMapper.getAllProjectNumByUciDbId(uciDbId));
        return info;
    }

    /**
     * 查看项目下所有订单信息
     * @param pid 项目id
     * @param state 订单状态 0-未完成订单 1-已完成订单
     * @return 所有订单信息
     */
    @Override
    public List<OrderInfo> getAllOrderInfoOfProject(String pid, String state) {
        List<OrderInfo> orderInfoList = orderInfoMapper.getAllByPId(pid);
        if (!CollectionUtils.isEmpty(orderInfoList)) {
            for (OrderInfo orderInfo : orderInfoList) {
                //设置订单状态 0-正常  1 异常
                orderInfo.setOrderState(orderInfo.getOrderStorageNumber().equals(orderInfo.getOrderDeliveryNumber()) ? "0" : "1");
                orderInfo.setOrderTotalMoney(orderInfo.getOrderGoodsMoney().add(orderInfo.getOrderServiceMoney()));
                //获取时效正常、异常数
                orderInfo.setTimeNormalNum(projectMapper.getTimeNormalNum(pid));
                orderInfo.setTimeAbnormalNum(projectMapper.getTimeAbnormalNum(pid));
                //获取合格、不合格物资数及总数
                orderInfo.setQualifiedMaterialNum(projectMapper.getMaterialNum(pid, "01"));
                orderInfo.setUnQuaMaterialNum(projectMapper.getMaterialNum(pid, "02"));
                orderInfo.setTotalMaterialNum(orderInfo.getQualifiedMaterialNum() + orderInfo.getUnQuaMaterialNum());
                orderInfo.setInventoryAmount(orderInfo.getOrderStorageMoney().subtract(orderInfo.getOrderDeliveryMoney()));
            }
            if ("0".equals(state)) {
                orderInfoList = orderInfoList.stream().filter(orderInfo -> "1".equals(orderInfo.getOrderState())).collect(Collectors.toList());
            } else if ("1".equals(state)) {
                orderInfoList = orderInfoList.stream().filter(orderInfo -> "0".equals(orderInfo.getOrderState())).collect(Collectors.toList());
            }
        }
        return orderInfoList;
    }

    /**
     * 查看项目下所有需求计划信息
     * @param pid 项目id
     * @param state 需求计划状态 0-未完成需求计划 1-已完成需求计划
     * @return 所有需求计划信息
     */
    @Override
    public List<ProjectPlanReqDetailMO> getAllPlanReqDetailInfoOfProject(String pid, String state) {
        List<ProjectPlanReqDetailMO> detailMOList = projectMapper.getAllPlanReqDetailInfoOfProject(pid);
        if (!CollectionUtils.isEmpty(detailMOList)) {
            for (ProjectPlanReqDetailMO detailMO : detailMOList) {
                //设置需求计划状态 0-已完成  1 未完成
                detailMO.setPlanReqState(detailMO.getPlanReqNum().equals(detailMO.getPlanAlreadyBuyNum()) ? "0" : "1");
            }
            if ("0".equals(state)) {
                detailMOList = detailMOList.stream().filter(detailMO -> "1".equals(detailMO.getPlanReqState())).collect(Collectors.toList());
            } else if ("1".equals(state)) {
                detailMOList = detailMOList.stream().filter(detailMO -> "0".equals(detailMO.getPlanReqState())).collect(Collectors.toList());
            }
        }
        return detailMOList;
    }


    /**
     * 根据项目id和工序查看物料清单
     * @param pid 项目id
     * @param planProcess 工序 0-完井 1-一开 2-二开 3-三开 4-四开 5-五开
     */
    @Override
    public List<ProjectPlanReqDetailMO> getAllInfoOfBOM(String pid, String planProcess) {
         List<ProjectPlanReqDetailMO> detailMOList = projectMapper.getAllInfoOfBOM(pid,planProcess);
        for (ProjectPlanReqDetailMO detailMO : detailMOList) {
            detailMO.setOrderId(projectMapper.getOrderIdByMatCode(pid,detailMO.getPlanMatCode()));
            detailMO.setMatAmount(projectMapper.getMatAmount(pid,detailMO.getMatCode()));
        }
        return detailMOList;
    }

    /**
     * 查询项目井别信息
     * @param pid 项目id
     */
    @Override
    public Project getWellInfo(String pid) {
        return projectMapper.getWellInfo(pid);
    }

    @Override
    public List<ReqMatNumber> getReqMatNumber(String pid) {
        ArrayList<ReqMatNumber> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            ReqMatNumber reqMatNumber = new ReqMatNumber();
            reqMatNumber.setProcess(i);
            reqMatNumber.setReqNumber(projectMapper.getReqCountOfBOM(pid, i + ""));
            list.add(reqMatNumber);
        }
        return list;
    }

    @Override
    public AllCompanyInfoVO getAllCompanyInfo() {
        AllCompanyInfoVO infoVO = projectMapper.getAllCompanyInfo();
        //物资概算总金额
        infoVO.setGeneralBudgetAmount(projectMapper.getGeneralBudgetAmount());
        //西油企业数
        infoVO.setXyCompanyTotal(projectMapper.getXyCompanyTotal());
        infoVO.setZyCompanyTotal(projectMapper.getZyCompanyTotal());
        infoVO.setUnZyCompanyTotal(projectMapper.getUnZyCompanyTotal());
        return infoVO;
    }

    @Override
    public ProjectPlanReqInfoVO getAllPlanReqInfoOfProject(String pid) {
        ProjectPlanReqInfoVO infoVO = projectMapper.getAllPlanReqInfoOfProject(pid);
        infoVO.setPlanReqState(projectMapper.getPlanReqState(pid) > 0 ? "1" : "0");
        //获取时效正常、异常数
        infoVO.setTimeNormalNum(projectMapper.getTimeNormalNum(pid));
        infoVO.setTimeAbnormalNum(projectMapper.getTimeAbnormalNum(pid));
        //获取合格、不合格物资数及总数
        infoVO.setQualifiedMaterialNum(projectMapper.getMaterialNum(pid, "01"));
        infoVO.setUnQuaMaterialNum(projectMapper.getMaterialNum(pid, "02"));
        infoVO.setTotalMaterialNum(infoVO.getQualifiedMaterialNum() + infoVO.getUnQuaMaterialNum());
        infoVO.setOrderStorageMoney(projectMapper.getOrderStorageMoney(pid));
        infoVO.setOrderDeliveryMoney(projectMapper.getOrderDeliveryMoney(pid));
        infoVO.setInventoryAmount(infoVO.getOrderStorageMoney().subtract(infoVO.getOrderDeliveryMoney()));
        return infoVO;
    }

    @Override
    public OrderInfoOfPro getOrderInfoOfProject(String pid, String state) {
        OrderInfoOfPro info = projectMapper.getOrderInfo(pid, state);
        //获取五大服务订单数量及金额
        int purchaseNum = purchaseMapper.getServiceNumByPid(pid);
        int settleNum = settleMapper.getServiceNumByPid(pid);
        int storageNum = storageMapper.getServiceNumByPid(pid);
        int transportNum = transportMapper.getServiceNumByPid(pid);
        int qualityNum = qualityMapper.getServiceNumByPid(pid);
        info.setTotalService(purchaseNum+settleNum+storageNum+transportNum+qualityNum);
        return info;
    }

    @Override
    public ManagerInfo getManagerInfo() {
        //采购总金额、销售总金额、物资概算总金额、订单数量、已下订单总金额、服务订单总金额
        ManagerInfo info = projectMapper.getManagerInfo();
        Map<String,Object> map = projectMapper.getReqInfo();
        //需求计划数
        info.setPlanReqCount(((Long)map.get("planReqCount")).intValue());
        info.setDoneReqCount(((Long)map.get("doneReqCount")).intValue());
        info.setUnDoneReqCount(((Long)map.get("unDoneReqCount")).intValue());
        return info;
    }
}
